package com.xcity.game.equip.suit4.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.buff.CombatBuffTemplate;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.combat.unit.impl.EmptyCombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.equip.suit4.BaseSuit4;
import com.xcity.game.equip.template.EquipSuitEffectTemplate;

/**
 * 华伦天奴4件套<br/>
 * 每有一个队友陷入崩溃状态增加自身{30}%伤害<br/>
 * @author yang.li
 *
 */
public class Suit4Valentino extends BaseSuit4 {
	
	private byte mask;
	private float damageRate = 1f;

	public Suit4Valentino(CombatUnit unit, EquipSuitEffectTemplate template) {
		super(unit, template);
	}

	@Override
	public void preDamage0(ActionEffect effect) {
		List<CombatUnit> units = unit.getTeam().all();
		Action act = null;
		for (int i = 0; i < units.size(); i++) {
			CombatUnit u = units.get(i);
			if (u != EmptyCombatUnit.INSTANCE
					&& u.getId() != unit.getId()
					&& u.isDied()
					&& (mask >> i & 1) == 0) {
				mask |= 1 << i;
//				float attackPower = unit.getAttackPower() * (1 + template.getSuit4Param(0) / GameStaticConfig.percentBase);
				damageRate += template.getSuit4Param(0) / GameStaticConfig.percentBase;
				if (act == null) {
					act = new Action(unit);
					effect.ref().ref().add(act);
				}
				act.addTarget(u);
				CombatBuffTemplate buffTemplate = CombatBuffTemplate.find(4);
				act.addBuff(new BUFF(buffTemplate, unit, damageRate));
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		private float damageRate;
		public BUFF(CombatBuffTemplate template, CombatUnit owner, float damageRate) {
			super(template, owner);
//			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, attackPower);
			this.damageRate = damageRate;
		}
		@Override
		public void preDamage0(ActionEffect effect) {
			Value v = effect.getValue(CombatAttributeType.HP);
			if (v != null && v.getDelta() < 0) {
				v.setDelta((int) (v.getDelta() * damageRate));
			}
		}
	}


}
